import {
    Component,
    OnInit,
    Injector,
    ViewChild,
    ViewEncapsulation,
} from "@angular/core";
import {
    ParkingServiceProxy,
    ParkingDto,
    CreateParkingDto,
    UpdateParkingDto,
} from "@shared/service-proxies/service-proxies";
import * as moment from "moment";
import { LazyLoadEvent } from "primeng/components/common/lazyloadevent";
import { Paginator } from "primeng/components/paginator/paginator";
import { Table } from "primeng/components/table/table";

import { finalize } from "rxjs/operators";

import { AppComponentBase } from "@shared/common/app-component-base";
import { appModuleAnimation } from "@shared/animations/routerTransition";
import { CreateOrEditParkingModalComponent } from "./create-or-edit-parking-modal.component";
import { Router } from "@angular/router";
import { MessageService } from "primeng/api";

@Component({
    selector: "app-parkings",
    templateUrl: "./parkings.component.html",
    styleUrls: ["./parkings.component.css"],
    animations: [appModuleAnimation()],
})
export class ParkingsComponent extends AppComponentBase implements OnInit {
    @ViewChild("dataTable") dataTable: Table;
    @ViewChild("paginator") paginator: Paginator;
    @ViewChild("createOrEditParkingModal")
    createOrEditParkingModal: CreateOrEditParkingModalComponent;

    constructor(
        injector: Injector,
        private route: Router,
        private _parkingServiceProxy: ParkingServiceProxy
    ) {
        super(injector);
    }

    ngOnInit() {}

    getParking(event?: LazyLoadEvent): void {
        this._parkingServiceProxy
            .getAll(
                this.primengTableHelper.getSorting(this.dataTable),
                this.primengTableHelper.getMaxResultCount(
                    this.paginator,
                    event
                ),
                this.primengTableHelper.getSkipCount(this.paginator, event)
            )
            .pipe(
                finalize(() => this.primengTableHelper.hideLoadingIndicator())
            )
            .subscribe((result) => {
                console.log(result);
                this.primengTableHelper.records = result.items;
                this.primengTableHelper.totalRecordsCount = result.items.length;
                this.primengTableHelper.hideLoadingIndicator();
                console.log(result.items);
            });
    }

    createParking(): void {
        this.createOrEditParkingModal.show();
    }
    updateParking(id: number): void {
        this.createOrEditParkingModal.show();
    }

    deleteParking(parking: ParkingDto): void {
        this.message.confirm(
            this.l("删除车场" + parking.parkingName + "?"),
            this.l("AreYouSure"),
            (isConfirmed) => {
                if (isConfirmed) {
                    this._parkingServiceProxy
                        .delete(parking.id)
                        .subscribe(() => {
                            this.getParking();
                            abp.notify.success(this.l("SuccessfullyDeleted"));
                        });
                }
            }
        );
    }
    guard(parking: ParkingDto): void {
        let parkingName = parking.parkingName;
        let parkingid = parking.id;
        let parkingCoce = parking.parkingCode;
        let neighbourhood_id = parking.neighbourhood_id;
        let neighbourhood_Name = parking.neighbourhood_Name;
        this.route.navigateByUrl(
            "app/vehicl-management/park-guard?parkingid=" +
                parkingid +
                "&parkingName=" +
                parkingName +
                "&parkingCoce=" +
                parkingCoce +
                "&neighbourhood_id=" +
                neighbourhood_id +
                "&neighbourhood_Name=" +
                neighbourhood_Name
        );
    }
    places(parking: ParkingDto): void {
        let parkingName = parking.parkingName;
        let parkingid = parking.id;
        let parkingCoce = parking.parkingCode;
        let neighbourhood_id = parking.neighbourhood_id;
        let neighbourhood_Name = parking.neighbourhood_Name;
        this.route.navigateByUrl(
            "app/vehicl-management/places?parkingid=" +
                parkingid +
                "&parkingName=" +
                parkingName +
                "&parkingCoce=" +
                parkingCoce +
                "&neighbourhood_id=" +
                neighbourhood_id +
                "&neighbourhood_Name=" +
                neighbourhood_Name
        );
    }
    parkingsBillingScheme(parking: ParkingDto): void {
        let parkingName = parking.parkingName;
        let parkingid = parking.id;
        let parkingCoce = parking.parkingCode;
        let neighbourhood_id = parking.neighbourhood_id;
        let neighbourhood_Name = parking.neighbourhood_Name;
        this.route.navigateByUrl(
            "app/vehicl-management/parkings-billing-scheme?parkingid=" +
                parkingid +
                "&parkingName=" +
                parkingName +
                "&parkingCoce=" +
                parkingCoce +
                "&neighbourhood_id=" +
                neighbourhood_id +
                "&neighbourhood_Name=" +
                neighbourhood_Name
        );
    }
}
